/**
 * 
 */
package com.easidea.tim.service.report;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.easidea.tim.dao.report.ReportDao;
import com.easidea.tim.entity.report.StorageYearlyReportDTO;

/**
 * 专供Flex端调用的统计报表Service对象，封装了统计报表转用的Dao对象，完成数据获取
 * 
 * @author Harrison
 *
 */
public class ReportService {

	private ReportDao reportDao;

	public List<StorageYearlyReportDTO> getStorageYearlyReport(Integer year){
		List<StorageYearlyReportDTO> rstList = null;
		List<StorageYearlyReportDTO> list = reportDao.getStorageYearlyReport(year);
		Map<Short,StorageYearlyReportDTO> monthSet = null;
		if(list != null){
			if(list.size() == 12){
				rstList = list;
			}else{
				rstList = new ArrayList<StorageYearlyReportDTO>();
				monthSet = getExistMonthSet(list);
				for(short i = 1; i<13;i++){
					if(!monthSet.containsKey(i)){
						StorageYearlyReportDTO dto = new StorageYearlyReportDTO();
						dto.setMonth((i<10?"0"+i:i)+"月");
						dto.setProductQuantity(0D);
						dto.setSampleQuantity(0D);
						rstList.add(dto);
					}else{
						rstList.add(monthSet.get(i));
					}
				}
			}
		}
		return rstList;
	}
	
	private Map<Short,StorageYearlyReportDTO> getExistMonthSet(List<StorageYearlyReportDTO> list) {
		Map<Short,StorageYearlyReportDTO> set = new HashMap<Short,StorageYearlyReportDTO>();
		for(StorageYearlyReportDTO dto : list){
			Short month = Short.valueOf(dto.getMonth().substring(0,2));
			set.put(month,dto);
		}
		return set;
	}

	/**
	 * @return the reportDao
	 */
	public ReportDao getReportDao() {
		return reportDao;
	}

	/**
	 * @param reportDao the reportDao to set
	 */
	public void setReportDao(ReportDao reportDao) {
		this.reportDao = reportDao;
	}
}
